//
//  RPlayHistoryTableViewCell.m
//  iMoive
//
//  Created by 侯伟 on 2021/9/1.
//

#import "RPlayHistoryTableViewCell.h"
#import "RVideoObj.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface RPlayHistoryTableViewCell (){
    UIImageView* _coverImage;
    UILabel* _totalTimeLb;
    UILabel* _nameLb;
    UILabel* _reduceTimeLb;
    UIButton* _playBtn;
    UIButton* _selectBtn;
    UIView* _bgView;
}

/// 显示内容视图
@property(nonatomic,weak) MyRelativeLayout* contentLayout;

@end

@implementation RPlayHistoryTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.backgroundColor = UIColor.clearColor;
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initSubViews];
        
        self.editable = NO;
    }
    return self;
}


- (void)initSubViews{
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.padding = UIEdgeInsetsMake(8, 10, 8, 10);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UIButton* selectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [selectBtn setImage:[UIImage imageNamed:@"common_select_n"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"common_select_h"] forState:UIControlStateSelected];
    selectBtn.mySize = CGSizeMake(PX(28), PX(28));
    selectBtn.myCenterY = 0;
    selectBtn.myLeft = 0;
    [selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [contentLayout addSubview:selectBtn];
    _selectBtn = selectBtn;
    
    UIView* bgView = [UIView new];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.myCenterY = 0 ;
    bgView.myRight = 0;
    bgView.myHeight = PX(120);
    bgView.leftPos.equalTo(selectBtn.rightPos).offset(10);
    bgView.layer.cornerRadius = PX(8);
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = UIColor.whiteColor;
    [contentLayout addSubview:bgView];
        
    MyRelativeLayout* converLayout = [MyRelativeLayout new];
    converLayout.backgroundColor = COLOR_BACKGEOUND;
    converLayout.myHeight = PX(157);
    converLayout.myWidth = PX(105);
    converLayout.myVertMargin = 0;
    converLayout.leftPos.equalTo(bgView.leftPos).offset(10);
    converLayout.layer.cornerRadius = PX(8);
    converLayout.layer.masksToBounds = YES;
    [contentLayout addSubview:converLayout];
    
    UIImageView* coverImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_placeholder"]];
    coverImage.contentMode = UIViewContentModeScaleAspectFill;
    coverImage.myMargin = 0;
    [converLayout addSubview:coverImage];
    _coverImage = coverImage;
    
    UILabel* totalTimeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_WHITE text:@"00:00:00"];
    totalTimeLb.textAlignment = NSTextAlignmentRight;
    totalTimeLb.bottomPos.equalTo(coverImage.bottomPos);
    totalTimeLb.myHorzMargin = 4;
    totalTimeLb.myBottom = 4;
    totalTimeLb.myHeight = PX(20);
    [converLayout addSubview:totalTimeLb];
    _totalTimeLb = totalTimeLb;
    
    
    MyRelativeLayout *infoLayout = [MyRelativeLayout new];
    infoLayout.padding = UIEdgeInsetsMake(8.0, 0, 8.0, 0);
    infoLayout.myHeight = PX(120);
    infoLayout.leftPos.equalTo(converLayout.rightPos).offset(10);
    infoLayout.rightPos.equalTo(bgView.rightPos).offset(10);
    infoLayout.myCenterY = 0;
    [contentLayout addSubview:infoLayout];
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_ORANGE text:@"影片名称"];
    nameLb.myHorzMargin = 0;
    nameLb.myHeight = MyLayoutSize.wrap;
    [infoLayout addSubview:nameLb];
    _nameLb = nameLb;
    
    UILabel* reduceTimeLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"00:00:00"];
    reduceTimeLb.topPos.equalTo(nameLb.bottomPos).offset(10.0);
    reduceTimeLb.numberOfLines = 2;
    reduceTimeLb.myHeight = MyLayoutSize.wrap;
    reduceTimeLb.myHorzMargin = 0;
    [infoLayout addSubview:reduceTimeLb];
    _reduceTimeLb = reduceTimeLb;
    
    UIButton* playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playBtn.userInteractionEnabled = NO;
    playBtn.backgroundColor = COLOR_THEME;
    [playBtn setTitle:@"立即播放" forState:UIControlStateNormal];
    [playBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
    playBtn.titleLabel.font = FONT_FIT(12);
    playBtn.myLeft = 0;
    playBtn.myBottom = 0;
    playBtn.mySize = CGSizeMake(PX(70), PX(24));
    playBtn.layer.cornerRadius = PX(12);
    [infoLayout addSubview:playBtn];
    _playBtn = playBtn;
}

- (void)setEditable:(BOOL)editable{
    _editable = editable;
    
    _selectBtn.visibility = editable?MyVisibility_Visible:MyVisibility_Gone;
    [_selectBtn sizeToFit];
}

- (void)setMovie:(RVideoObj *)movie{
    _movie = movie;
    
    if(self.editable){
        _selectBtn.selected = movie.isSelected;
        [_selectBtn sizeToFit];
    }
    
    //封面
    [_coverImage sd_setImageWithURL:[NSURL URLWithString:movie.img] placeholderImage:[UIImage imageNamed:@"common_placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if(error){
            [[SDWebImageManager sharedManager] removeFailedURL:imageURL];
        }
    }];
    
    // 总时长
    _totalTimeLb.text = movie.info;//totalTime;
    [_totalTimeLb sizeToFit];
    
    //影片名称
    _nameLb.text = [NSString stringWithFormat:@"%@ %@",movie.name,movie.dramaName?movie.dramaName:@""];
    [_nameLb sizeToFit];
    
    //剩余
    NSString *reduceTime = nil;
    long reduceSecond = movie.totalTime - movie.currentTime;
    if (movie.currentTime < 60) {
        _reduceTimeLb.text = @"观看少于一分钟";
    } else if(reduceSecond >= 60 && reduceSecond < 3600){
        reduceTime = [NSString stringWithFormat:@"%02zd:%02zd", reduceSecond/60, reduceSecond%60];
        _reduceTimeLb.text = [NSString stringWithFormat:@"剩余：%@",reduceTime];
        [_reduceTimeLb sizeToFit];
    } else if(reduceSecond >= 3600){
        reduceTime = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", reduceSecond/3600, reduceSecond%3600/60, reduceSecond%60];
        _reduceTimeLb.text = [NSString stringWithFormat:@"剩余：%@",reduceTime];
        [_reduceTimeLb sizeToFit];
    }
}

- (void)selectClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    
    self.movie.isSelected = sender.selected;
    
    if(self.selectBlock){
        self.selectBlock(self.movie.isSelected);
    }
}
@end
